<?PHP
    if (!defined("__PLAYPLUS__")) exit;

    function datediff () {
        global $ms;
    }

    /**
    * @brief  Error Function
    * @param  string  msg    Message
    * @param  string  url    URL
    * @return string  Script
    * @access public
    * @author Magicsoft (developers@magicsoft.com)
    * @date   2013-04-03
    **/
    function error ($msg, $url) {
        $return='<script type="text/javascript">';
        $return.='alert("'.$msg.'");';
        $return.='window.location.replace("'.$url.'")';
        $return.='</script>';

        echo $return;
    }

    /**
    * @brief  Admin Check Function
    * @return bool  TRUE or FALSE
    * @access public
    * @author Magicsoft (developers@magicsoft.com)
    * @date   2013-04-03
    **/
    function is_admin () {
        global $user;

        if (is_login() && $user['u_level']<2)
            return 1;
        else
            return 0;
    }

    /**
    * @brief  Login Check Function
    * @return bool  TRUE or FALSE
    * @access public
    * @author Magicsoft (developers@magicsoft.com)
    * @date   2013-04-03
    **/
    function is_login () {
        global $user;

        if ($user['u_code'])
            return 1;
        else
            return 0;
    }

    /**
    * @brief  Find Image In Contents Function
    * @param  string  val    Contents
    * @return string  NULL or Image Source
    * @access public
    * @author Magicsoft (developers@magicsoft.com)
    * @date   2013-04-03
    **/
    function get_image($val) {
        $image="";
        preg_match("<[iI][mM][gG] [^<>]*>", $val, $imageTag);

        if($imageTag[0]){
            if(stristr($imageTag[0], "http://")){
                preg_match("/http:\/\/.*\.(jp[e]?g|gif|png)/Ui", $imageTag[0], $imageName);
                $image=$imageName[0];
            } else {
                preg_match("/\/.*\.(jp[e]?g|gif|png)/Ui", $imageTag[0], $imageName);
                $image=$imageName[0];
            }
        }

        return $image;
    }

    function boardCount ($query, $now_page) {
        global $pp;
        $mysql=new mysql;

        $return['now_page']=$now_page;
        $return['total_record']=$mysql->num_rows($query);
    	$return['total_page']=ceil($return['total_record'] / $pp['page_per_record']);
    	$return['total_block']=ceil($return['total_page'] / $pp['block_per_page']);
    	$return['now_block']=ceil($return['now_page'] / $pp['block_per_page']);
    	$return['start_record']=(($return['now_page']-1) * $pp['page_per_record']);
    	$return['start_page']=(($return['now_block']-1) * $pp['block_per_page']) +1;
    	if ($return['total_page'] < ($pp['block_per_page']*$return['now_block']))
    		$return['end_page']=$return['total_page'];
        else
    		$return['end_page']=($return['start_page']-1)+$pp['block_per_page'];
        $return['no']=$return['total_record']-(int)(($return['now_page']-1)*$pp['page_per_record']);

        return $return;
    }

    function getPaging ($writePages, $nowPage, $totalPage, $url, $add="") {
        $startPage=(((int)(($nowPage-1)/$writePages))*$writePages)+1;
		$endPage=$startPage+$writePages-1;

        $return='<div class="paging">';
		if ($nowPage > 1)
            $return.='<li class="nex"><a href="'.$url.'?page=1'.$add.'">처음</a></li>';
		if ($endPage >= $totalPage)
            $endPage=$totalPage;
        if ($startPage > 1)
            $return.='<li class="nex"><a href="'.$url.'?page='.($startPage-1).$add.'">이전</a></li>';
        for ($i=$startPage; $i<=$endPage; $i++) {
            if ($nowPage != $i)
                $return.='<li class=""><a href="'.$url.'?page='.$i.$add.'">'.$i.'</a></li>';
            else
                $return.='<li class="on"><a href="'.$url.'?page='.$i.$add.'">'.$i.'</a></li>';
        }
        if ($totalPage > $endPage && $endPage > 1)
            $return.='<li class="nex"><a href="'.$url.'?page='.($endPage+1).$add.'">다음</a></li>';
        if ($nowPage < $totalPage)
            $return.='<li class="nex"><a href="'.$url.'?page='.$totalPage.$add.'">끝</a></li>';
        $return.='</div>';

		return $return;
    }
?>